if (typeof(TMR_WEBCHAT_NICKNAME) == 'undefined') TMR_WEBCHAT_NICKNAME = '';
if (typeof(TMR_WEBCHAT_BOT) == 'undefined') TMR_WEBCHAT_BOT = 'ikitobot';
if (typeof(TMR_WEBCHAT_LIMIT) == 'undefined') TMR_WEBCHAT_LIMIT = 100;

TMR_WEBCHAT_BASE_URL = 'http://tmrweb.ikito.ru/chat/';
TMR_WEBCHAT_FORM = 'tmr_webchat_form';
TMR_WEBCHAT_TEXTS = 'tmr_webchat_texts_container';
TMR_WEBCHAT_NICK = 'tmr_webchat_nick';
TMR_WEBCHAT_MSG = 'tmr_webchat_msg';
if (typeof(FAKE_WEBCHAT_NICKS) == 'undefined') FAKE_WEBCHAT_NICKS = true;

function load_css(css_name) {
    var css = document.createElement('link');
    css.rel = "stylesheet";
    css.type = "text/css";
    css.href = TMR_WEBCHAT_BASE_URL + css_name;
    css.media = "screen, projection";
    document.getElementsByTagName("head")[0].appendChild(css);
};

function load_js(url, js_id) {
    url = TMR_WEBCHAT_BASE_URL + url;
    if (js_id == undefined) js_id = 'some_script';
    var script=document.createElement('script');
    script.id = js_id;
    script.type = "text/javascript";
    uniq_string = js_id+Math.random().toString().replace(/\./g,"");

    if (url.indexOf("?") > 0) delim = '&'; else delim = '?';
    script.src = url + delim + uniq_string;
    document.getElementsByTagName("head")[0].appendChild(script);
};

function update_js(url, js_id) {
    if (js_id == undefined) js_id = 'some_script';
    var e=document.getElementById(js_id); 
    if (e != undefined) e.parentNode.removeChild(e);
    load_js(url, js_id);
    //scriptId="updChat"+Math.random().toString().replace(/\./g,"");
};

function draw_webchat_controls() {
    html = " \
<div id='tmr_webchat'> \
<div id='" + TMR_WEBCHAT_TEXTS + "'> \
It's going to be legen... Wait for it... Dary! \
</div> \
<div id='tmr_webchat_form_container'> \
    <form id='" + TMR_WEBCHAT_FORM + "' class='form-inline'> \
        <input id='" + TMR_WEBCHAT_NICK + "' type='text' value='" + TMR_WEBCHAT_NICKNAME + "' size='15' maxlength='15' class='input-small'> \
        <input id='" + TMR_WEBCHAT_MSG + "' type='text' value='' size='50' maxlength='1048' autocomplete='off'> \
        <input class='btn' type='submit' value='Отправить'> \
        <input id='play_chat_sound' type='checkbox' title='Звуковое оповещение о сообщении в чате' checked> \
        <input id='simulate_send_to_chat' type='checkbox' title='Симулировать отправку в чат'> \
    </form> \
</div> \
</div> \
    ";
    document.write(html);
}

function webchat_core_loaded() {
    tmr_webchat_updater = window.setInterval(update_tmr_webchat, 3000);
    document.forms[TMR_WEBCHAT_FORM].onsubmit = function(){send_to_chat_via_post(); return false;};
    document.getElementById(TMR_WEBCHAT_MSG).focus();
};

draw_webchat_controls();
load_css('css/webchat.css');
if (typeof($) == 'undefined') load_js('js/jquery.min.js');
load_js('js/webchat_core.js');

// load_js('/js/webchat.js');
$('body').append(' \
    <audio id="playSound"> \
        <source src="/chat/static/notify.mp3" type="audio/mp3"\> \
        <source src="/chat/static/notify.ogg" type="audio/ogg"\> \
    </audio> \
');
function play_sound(audio_link) {
    if (!document.getElementById('play_chat_sound').checked) return;
    var player = document.getElementById('playSound');
    player.play();
};

